package com.laihui.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.laihui.MyApplication;
import com.laihui.base.BaseActivity;
import com.laihui.dialog.TenderDialog;
import com.laihui.entity.LoanBean;
import com.laihui.entity.LoanItem;
import com.laihui.fragment.DetailFragment;
import com.laihui.fragment.LoanPicFragment;
import com.laihui.fragment.TenderListFragment;

import com.laihui.view.TopBar;

import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class InvestContentActivity extends BaseActivity {

    @BindView(R.id.tabs)
    TabLayout tabs;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    @BindView(R.id.btn_total_tender)
    Button button;
    @BindView(R.id.tv_wait_tender)
    TextView wait_tender;
    @BindView(R.id.tv_apr)
    TextView tv_apr;
    @BindView(R.id.tv_loan_lowest_account)
    TextView lowest_account;

    TenderDialog dialog;

    @BindView(R.id.topbar)//topbar
            TopBar topbar;


    LoanBean loanbean;
//    InvestDetail investDetail;

    private Activity mActivity;
    private MyApplication application;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    //private ArrayList<String> titles = new ArrayList<>();
    private List<String> mTitles = new ArrayList<>();

    public static Object[] list = new Object[10];
    private DecimalFormat df = new DecimalFormat("0.00");
    LoanBean loan;
    @Override
    public void init() {
        mActivity = InvestContentActivity.this;
        application = (MyApplication) getApplication();
        topbar.setOnTopbarClickListener(new TopBar.topbarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

        //showProgress();
        loan = (LoanBean) getIntent().getSerializableExtra("loan");
        addTitle(mTitles);
        mFragments= getFragments(loan);
        viewPager.setAdapter(new InvestContentActivity.RecordPagerAdapter(getSupportFragmentManager()));
        initViewPager();
        tabs.setupWithViewPager(viewPager);
        tabs.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(tabs,20,20);
            }
        });


    }



    private class RecordPagerAdapter extends FragmentPagerAdapter {

        public RecordPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles.get(position);
        }
    }

    private void initViewPager() {
        HashMap map = new HashMap();
        map.put("loanId",loan.id+"");
        application.apiService.loanDetail(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LoanItem>() {
                    @Override
                    public void onCompleted() {

                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e("Index3Fragment", "onError: " + e.toString());
                    }

                    @Override
                    public void onNext(LoanItem loanItem) {
                       tv_apr.setText(loanItem.item.apr+"");
                        wait_tender.setText(Double.parseDouble(loanItem.item.account)-Double.parseDouble(loanItem.item.amount_invested)+"");
                        lowest_account.setText(loanItem.item.min_account+"起投");
                        loanbean = loanItem.item;
                    }
                });
    }

    public void setIndicator (TabLayout tabs,int leftDip,int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }

    private ArrayList<Fragment> getFragments(LoanBean loan) {
        ArrayList<Fragment> list = new ArrayList<>();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data",loan);//这里的values就是我们要传的值
        DetailFragment fragment1 = new DetailFragment();
        LoanPicFragment fragment2 = new LoanPicFragment();
        TenderListFragment fragment3 = new TenderListFragment();
        fragment1.setArguments(bundle);
        fragment2.setArguments(bundle);
        fragment3.setArguments(bundle);
        list.add(fragment1);
        list.add(fragment2);
        list.add(fragment3);
        return list;
    }



    private void addTitle(List<String> mTitles) {
        mTitles.add("基础信息");
        mTitles.add("审核资料");
        mTitles.add("投资记录");
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_loan_content;
    }

    @Override
    public Activity bindActivity() {
        return this;
    }

    @OnClick({ R.id.btn_total_tender})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.btn_total_tender://立即购买
                Intent intent = new Intent(mActivity, InvestActivity.class);
                intent.putExtra("loan", loanbean);
                startActivity(intent);
                break;
        }
    }
}
